package com.example.khandasawanat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.flurry.org.codehaus.jackson.map.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zooz.android.lib.CheckoutActivity;
import com.zooz.android.lib.model.ZooZInvoice;

public class MainActivity extends FragmentActivity {
	private static final int ZooZ_Activity_ID = 1;
	private static final String TAG = MainActivity.class.getSimpleName();
	// FragmentAdapter mFragmentAdapter;
	String[] menuSubs = { "Beer", "Sandwiches" };
	ViewPager mViewPager;
	FragmentAdapter mSectionsPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		String readFeed1 = loadJSONFromAsset();
		List<Restaurant> restaurantList = new Gson().fromJson(readFeed1, new TypeToken<List<Restaurant>>(){}.getType());
		mSectionsPagerAdapter = new FragmentAdapter(getSupportFragmentManager());
		mSectionsPagerAdapter.setRestaurantList(restaurantList);
		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onCheckoutClick(View v) {
		ZooZInvoice invoice = new ZooZInvoice();
		invoice.addItem("Mushroom", 24, 2.55, "0125G-1", "24 for 2.55 Sale");
		invoice.addItem("Coin", 1, 0.99, null, null);
		invoice.setInvoiceNumber("5512-FA");
		invoice.setInvoiceAdditionalDetails("Power Ups for user 12345");
		// create new intent CheckoutActivity
		Intent intent = new Intent(this, CheckoutActivity.class);
		// supply app-key on the intent
		intent.putExtra(CheckoutActivity.ZOOZ_APP_KEY,
				"4339cf05-448e-41e9-8442-3f7475c06bfe");
		// supply transaction details (amount, currency)
		intent.putExtra(CheckoutActivity.ZOOZ_AMOUNT, 33.5);
		intent.putExtra(CheckoutActivity.ZOOZ_CURRENCY_CODE, "USD");
		intent.putExtra(CheckoutActivity.ZOOZ_INVOICE, invoice);
		// supply environment mode (sandbox or production)
		intent.putExtra(CheckoutActivity.ZOOZ_IS_SANDBOX, true);
		startActivityForResult(intent, ZooZ_Activity_ID);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == ZooZ_Activity_ID) {

			switch (resultCode) {
			case Activity.RESULT_OK:
				Log.i(TAG,
						"Succefuly paid. Your transaction id is: "
								+ data.getStringExtra(CheckoutActivity.ZOOZ_TRANSACTION_ID)
								+ "\nDisplay ID: "
								+ data.getStringExtra(CheckoutActivity.ZOOZ_TRANSACTION_DISPLAY_ID)
								+ data.getStringExtra(CheckoutActivity.ZOOZ_USER_EMAIL));
				break;
			case Activity.RESULT_CANCELED:

				if (data != null)
					Log.e(TAG,
							"Error, cannot complete payment with ZooZ. "
									+ "Error code: "
									+ data.getIntExtra(
											CheckoutActivity.ZOOZ_ERROR_CODE, 0)
									+ "; Error Message: "
									+ data.getStringExtra(CheckoutActivity.ZOOZ_ERROR_MSG));
				break;

			default:
				break;
			}
		}
	}

//	public class FragmentAdapter extends FragmentPagerAdapter {
//
//		public FragmentAdapter(FragmentManager fm) {
//			super(fm);
//		}
//
//		@Override
//		public Fragment getItem(int position) {
//			// getItem is called to instantiate the fragment for the given page.
//			// Return a DummySectionFragment (defined as a static inner class
//			// below) with the page number as its lone argument.
//			Fragment fragment = new SubMenuFragment();
//			Bundle args = new Bundle();
//			args.putInt(SubMenuFragment.ARG_SECTION_NUMBER, position + 1);
//			fragment.setArguments(args);
//			return fragment;
//		}
//
//		@Override
//		public int getCount() {
//			// Show 3 total pages.
//			return menuSubs.length;
//		}
//
//		@Override
//		public CharSequence getPageTitle(int position) {
//			Locale l = Locale.getDefault();
//			return menuSubs[position];
//			// return null;
//		}
//	}

//	public class SubMenuFragment extends Fragment {
//		/**
//		 * The fragment argument representing the section number for this
//		 * fragment.
//		 */
//		public static final String ARG_SECTION_NUMBER = "section_number";
//
//		public SubMenuFragment() {
//		}
//
//		@Override
//		public View onCreateView(LayoutInflater inflater, ViewGroup container,
//				Bundle savedInstanceState) {
//			View rootView = inflater.inflate(R.layout.sub_menu_fragment,
//					container, false);
//			TextView dummyTextView = (TextView) rootView
//					.findViewById(R.id.section_label);
//			dummyTextView.setText(Integer.toString(getArguments().getInt(
//					ARG_SECTION_NUMBER)));
//			return rootView;
//		}
//	}

	public String loadJSONFromAsset() {
		String json = null;
		try {

			InputStream is = getAssets().open("test.json");

			int size = is.available();

			byte[] buffer = new byte[size];

			is.read(buffer);

			is.close();

			json = new String(buffer, "UTF-8");

		} catch (IOException ex) {
			ex.printStackTrace();
			return null;
		}
		return json;

	}

}
